<template>
  <div class="linkpage">
    <el-button class="addbtn" type="primary" plain icon="el-icon-plus" size="mini" @click="addlink('add')">新增</el-button>
    <el-tabs v-model="activeName">
      <el-tab-pane label="文字链接" name="first">
        <div class="table-box">
          <el-table id="outTable" ref="myTable" :data="wordlist" style="width: 100%" border>
            <el-table-column type="index" label="#" width="50" />
            <el-table-column prop="title" label="标题" />
            <el-table-column prop="des" label="链接描述" />
            <el-table-column label="链接类型">
              <template #default="{row}">
                <div class="type">{{ row.type==='1'?'文字类型':'图片类型' }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="url" label="链接地址" />
            <el-table-column prop="" label="操作">
              <template #default="{row}">
                <div style="display: flex; justify-content: center; align-items: center;">
                  <el-button size="mini" circle type="primary" icon="el-icon-edit" @click="addlink(row.id)" />
                  <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="dodelete(row.id)" />
                </div>
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页区域 -->
        <div class="page">
          <CutPage v-if="isTableAlive" :total="total1" :current-page="params1.page" :limit="params1.limit" @perpage="perpage($event,1)" @docurrentpage="docurrentpage($event,1)" />
        </div>
      </el-tab-pane>
      <el-tab-pane label="图片链接" name="second">
        <!-- 表格区域 -->
        <div class="table-box">
          <el-table id="outTable" ref="myTable" :data="picslist" style="width: 100%" border>
            <el-table-column type="index" label="#" width="50" />
            <el-table-column prop="des" label="链接描述" />
            <el-table-column prop="" label="链接图片">
              <template #default="{row}">
                <el-image style="width: 100px; height: 100px" :src="'http://124.223.14.236:8060/'+row.img" :fit="'cover'" />
              </template>
            </el-table-column>
            <el-table-column label="链接类型">
              <template #default="{row}">
                <div class="type">{{ row.type==='1'?'文字类型':'图片类型' }}</div>
              </template>
            </el-table-column>
            <el-table-column prop="url" label="链接地址" />
            <el-table-column prop="" label="操作">
              <template #default="{row}">
                <el-button size="mini" circle type="primary" icon="el-icon-edit" @click="addlink(row.id)" />
                <el-button size="mini" type="danger" icon="el-icon-delete" circle @click="dodelete(row.id)" />
              </template>
            </el-table-column>
          </el-table>
        </div>
        <!-- 分页区域 -->
        <div class="page">
          <CutPage v-if="isTableAlive" :total="total2" :current-page="params2.page" :limit="params2.limit" @perpage="perpage($event,2)" @docurrentpage="docurrentpage($event,2)" />
        </div>
      </el-tab-pane>
    </el-tabs>
    <el-dialog ref="tagdialog" :title="dialog" :visible.sync="dialogVisible" width="45%" @close="handleClose">
      <el-form ref="advlinkform" :model="advlinkform" label-width="100px" class="demo-dynamic" size="medium">
        <el-form-item prop="type" label="链接类型" :rules="{ required: true, message: '请输入链接类型', trigger: 'change' }">
          <el-row>
            <el-col :span="15"><el-select v-model="advlinkform.type" placeholder="请选择">
              <el-option label="文字链接" :value="'1'" />
              <el-option label="图片链接" :value="'2'" />
            </el-select></el-col>
          </el-row>
        </el-form-item>
        <el-form-item v-if="advlinkform.type==='1'" prop="title" label="标题" :rules="{ required: true, message: '请输入标题', trigger: 'blur' }">
          <el-row>
            <el-col :span="15"> <el-input v-model="advlinkform.title" /></el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="url" label="链接地址" :rules="{ required: true, message: '请输入链接地址', trigger: 'blur' }">
          <el-row>
            <el-col :span="15"> <el-input v-model="advlinkform.url" /></el-col>
          </el-row>
        </el-form-item>

        <el-form-item prop="des" label="链接描述" :rules="{ required: true, message: '请输入链接描述', trigger: 'blur' }">
          <el-row>
            <el-col :span="15"><el-input v-model="advlinkform.des" type="textarea" /></el-col>
          </el-row>
        </el-form-item>
        <el-form-item v-if="advlinkform.type==='2'" prop="img" label="链接图片" :rules="{ required: true, message: '请输入链接图片', trigger: 'change' }">
          <el-row>
            <el-col :span="15"><el-upload
              class="upload-demo"
              action="http://124.223.14.236:8060/admin/common/upload?type=images"
              :limit="1"
              :headers="headers"
              :on-success="fileSuccess"
              :file-list="fileList"
              :on-remove="fileremove"
              list-type="picture"
            >
              <el-button size="small" type="primary">点击上传</el-button>
            </el-upload></el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="small" @click="handleClose">取 消</el-button>
        <el-button size="small" type="primary" @click="submitForm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getlink, addadvlink, deladvlink, getadvlinkdetail, editadvlink } from '@/api/adv'
import CutPage from '@/components/Cutpage'
export default {
  components: {
    CutPage
  },
  data() {
    return {
      fileList: [],
      dialog: '添加链接',
      dialogVisible: false,
      currentid: '',
      advlinkform: {
        des: '',
        img: '',
        title: '',
        type: '',
        url: ''
      },
      wordlist: [],
      picslist: [],
      isTableAlive: true,
      params1: {
        type: 1,
        page: 1,
        limit: 10
      },
      params2: {
        type: 2,
        page: 1,
        limit: 10
      },
      activeName: 'first',
      total1: 0,
      total2: 0
    }
  },
  computed: {
    headers() {
      return {
        'X-Token': this.$store.getters.token
      }
    }
  },
  created() {
    this.getlinklist()
  },
  methods: {
    perpage(val, id) {
      if (id === 2) {
        this.params2.page = 1
        this.params2.limit = val
      } else {
        this.params1.page = 1
        this.params1.limit = val
      }
      this.getlinklist()
    },
    docurrentpage(val, id) {
      if (id === 2) {
        this.params2.page = val
      } else {
        this.params1.page = val
      }
      this.getlinklist()
    },
    dodelete(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await deladvlink(id)
        console.log(res)
        if (res === '') {
          if (this.wordlist.length === 1 || this.picslist.length === 1 && this.params.page > 1) {
            this.params.page--
          }
          this.$message.success('删除成功')
          this.getlinklist()
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    submitForm() {
      this.$refs.advlinkform.validate(
        async(valid) => {
          if (!valid) return

          if (this.dialog === '添加友情链接') {
            const res = await addadvlink(this.advlinkform)
            console.log(res)
            if (res === '') {
              this.$message.success('添加成功')
              this.dialogVisible = false
              this.getlinklist()
            }
          } else {
            const res = await editadvlink({ ...this.advlinkform, id: this.currentid })
            console.log(res)
            if (res === '') {
              this.$message.success('修改成功')
              this.handleClose()
              this.getlinklist()
            }
          }
        }
      )
    },
    fileSuccess(response, file, fileList) {
      console.log(response.data.savePath)
      this.fileList = fileList
      console.log(this.fileList)
      this.advlinkform.img = response.data.savePath
      this.$refs.advlinkform.validate()
    },
    fileremove(file, fileList) {
      console.log(fileList, file)
      this.fileList = fileList
      this.$refs.advlinkform.validate()
    },
    async addlink(id) {
      if (id === 'add') {
        this.dialog = '添加友情链接'
        // console.log(1)
        this.dialogVisible = true
      } else {
        this.dialog = '编辑友情链接'
        this.currentid = id
        const res = await getadvlinkdetail(id)
        console.log(res)
        this.advlinkform = res
        this.fileList = [{ url: 'http://124.223.14.236:8060/' + res.img }]
        this.dialogVisible = true
      }
    },
    async getlinklist() {
      const res1 = await getlink(this.params1)
      const res2 = await getlink(this.params2)
      console.log(res1, res2)
      this.wordlist = res1.data
      this.total1 = res1.count
      this.picslist = res2.data
      this.total2 = res2.count
    },
    // handleSizeChange1(val) {
    //   console.log(`每页 ${val} 条`)
    //   this.params1.page = 1
    //   this.params1.limit = val
    //   this.getlinklist()
    // },
    // handleCurrentChange1(val) {
    //   console.log(`当前页 ${val}`)
    //   this.params1.page = val
    //   this.getlinklist()
    // },
    // handleSizeChange2(val) {
    //   console.log(`每页 ${val} 条`)
    //   this.params2.page = 1
    //   this.params2.limit = val
    //   this.getlinklist()
    // },
    // handleCurrentChange2(val) {
    //   console.log(`当前页 ${val}`)
    //   this.params2.page = val
    //   this.getlinklist()
    // },
    handleClose() {
      this.dialogVisible = false
      this.$refs.advlinkform.resetFields()
    }
  }
}
</script>
<style scoped lang="scss">
.el-pagination {
  text-align: right;
}
::v-deep .addbtn {
  position: absolute;
  top: 25px;
  right: 20px;
  z-index: 999;
}
.linkpage {
  padding: 10px;
}
body {
  background-color: #eaedf1;
}
.el-tabs {
  background-color: #fff;
  padding: 15px;
}
.type {
  border: 1px solid #d9ecff;
  color: #409eff;
  background-color: #ecf5ff;
  text-align: center;
  border-radius: 5px;
  width: 60px;
  height: 30px;
  font-size: 12px;
  line-height: 30px;
  margin: auto;
}
.demo-dynamic {
  margin-left: 60px;
}
</style>
